package LeetCode.month02.day43;

import java.util.*;

public class No49 {
    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> res = new ArrayList<>();
        Map<String,List<String>> map = new HashMap<>();
        int n = strs.length;
        if(n==0) return res;
        for (int i = 0; i < n; i++) {
            char[] chars = strs[i].toCharArray();
            Arrays.sort(chars);
            String s = new String(chars);
            if(!map.containsKey(s)){
                List<String> list = new ArrayList<>();
                list.add(strs[i]);
                map.put(s,list);
            }else{
                List<String> list= map.get(s);
                list.add(strs[i]);
                map.put(s,list);
            }
        }
        res.addAll(map.values());
        return res;
    }
}
